package com.yiwei.zhh.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.yiwei.zhh.R;
import com.yiwei.zhh.util.DisplayUtil;


/**
 * Created by Administrator on 2017/3/14 0014.
 */

public class BottomIconText extends RelativeLayout {



    ImageView imageView;

    TextView textView;

    TextView textView2;

    public BottomIconText(Context context) {
        super(context, null);
        init(context, null);
    }

    public BottomIconText(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        init(context, attrs);
    }

    public BottomIconText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    //    private int textColor = 0xFF191919;
    private String textString = "";
    private String textContentString = "";
    private int textSize = DisplayUtil.dip2px(getContext(), 13);
    private int textContentSize = DisplayUtil.dip2px(getContext(), 11);
    private int iconHeight;
    private int iconWidth;
    private ColorStateList textColor;
    private ColorStateList textContentColor;
    private Drawable iconDrawable;
    private int textMarginTop;
    private int layout;

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomIconText);
        try {
            textColor = a.getColorStateList(R.styleable.BottomIconText_text_color);
            textString = a.getString(R.styleable.BottomIconText_text_string);
            textSize = a.getDimensionPixelSize(R.styleable.BottomIconText_text_size, textSize);
            textContentColor = a.getColorStateList(R.styleable.BottomIconText_text_content_color);
            textContentString = a.getString(R.styleable.BottomIconText_text_content_string);
            textContentSize = a.getDimensionPixelSize(R.styleable.BottomIconText_text_content_size, textContentSize);
            iconWidth = a.getDimensionPixelSize(R.styleable.BottomIconText_icon_width, 35);
            iconHeight = a.getDimensionPixelSize(R.styleable.BottomIconText_icon_height, 35);
            iconDrawable = a.getDrawable(R.styleable.BottomIconText_icon_drawable);
            textMarginTop = a.getDimensionPixelSize(R.styleable.BottomIconText_text_margin_top, 0);
            layout = a.getResourceId(R.styleable.BottomIconText_layout_id, R.layout.item_bottom_tab);
        } finally {
            a.recycle();
        }
        inflate(context, layout, this);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        imageView= (ImageView) findViewById(R.id.icon);
        textView= (TextView) findViewById(R.id.text1);
        textView2=(TextView) findViewById(R.id.text2);
        ViewGroup.LayoutParams imgParams = imageView.getLayoutParams();
        imgParams.width = iconWidth;
        imgParams.height = iconHeight;
        imageView.setLayoutParams(imgParams);
        if (iconDrawable != null) {
            iconDrawable.setBounds(0, 0, iconWidth, iconHeight);
            imageView.setImageDrawable(iconDrawable);
        }
        if(textMarginTop!=0){
            MarginLayoutParams text1Params =(MarginLayoutParams) textView.getLayoutParams();
            text1Params.setMargins(0,textMarginTop,0,0);
        }
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        if (textColor != null) {
            textView.setTextColor(textColor);
        }
        if (textString != null)
            textView.setText(textString);
        if (TextUtils.isEmpty(textContentString)) {
            textView2.setVisibility(View.GONE);
        } else {
            textView2.setVisibility(View.VISIBLE);
//            LayoutParams lp2 = (LayoutParams) textView2.getLayoutParams();
//            lp2.setMargins(0, textMarginTop, 0, 0);
//            textView2.setLayoutParams(lp2);
            textView2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textContentSize);
            textView2.setText(textContentString);
            if (textContentColor != null) {
                textView2.setTextColor(textContentColor);
            }
        }
    }


    public BottomIconText setDrawable(Drawable icon, int width, int height) {
        ViewGroup.LayoutParams imgParams = imageView.getLayoutParams();
        imgParams.width = width;
        imgParams.height = height;
        imageView.setLayoutParams(imgParams);
        if (icon != null) {
            icon.setBounds(0, 0, width, height);
            imageView.setImageDrawable(icon);
        }
        return this;
    }

    public BottomIconText setDrawable(Drawable icon) {
        if (icon != null) {
            imageView.setImageDrawable(icon);
        }
        return this;
    }
    public BottomIconText setTextString(String text, int size, int color) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        textView.setText(text);
        return this;
    }
}
